<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>

        <el-col :span="12">
          <el-form-item :label="$t('外派结束时间')" prop="expatsEndTime">
            <DatePicker v-model="form.expatsEndTime" :placeholder="$t('外派结束时间')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <el-button @click="cancel()">{{ $t("取消") }}</el-button>
      <el-button type="primary" @click="submitForm()">{{ $t("保存") }}
      </el-button>
    </div>
  </div>
</template>

<script>
import { getDealExpats, saveDealExpats } from '@/api/deal/expats/dealExpats'

export default {
  data() {
    return {
      // 菜单标识
      menuKey: 'deal_expats',
      // 是否隐藏保存按钮
      btnHide: false,
      // 是否隐藏其他按钮
      otherBtnHide: false,
      form: {
        fileList: [],
        user: {
          userId: undefined
        }
      },
      // 表单校验
      rules: {
        expatsEndTime: [
          { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
        ]
      },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true
    }
  },
  created() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDealExpats(data.id).then(response => {
          this.form = response.data
          this.form.expatsEndTime = new Date()
          this.loading = false
        })
      } else {
        this.reset()
        this.getDealType(this.$route.query.id)
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        user: {
          userId: undefined
        },
        dealId: this.$route.query.id,
        level: undefined,
        expatsTime: undefined,
        expatsAddress: undefined,
        explain: undefined,
        createBy: undefined,
        createTime: undefined,
        fileList: [],
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        actId: undefined,
        actStatus: undefined,
        expatsEndTime: new Date(),
        finish: 'Y'
      }
      this.resetForm('form')
    },

    // 取消按钮
    cancel() {
      this.$parent.$parent.endOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.endOpen !== undefined) {
        this.$parent.$parent.endOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 是否外派修改
            this.form.finish = 'Y'
            // 保存数据
            saveDealExpats(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('外派结束时间：' + this.form.expatsEndTime))
                // 关闭form页面
                this.closeForm()
                resolve()
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject()
              }
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.locationError(document)
          }
        })
      })
    }

  }

}
</script>
